package pl.vanta.mjuzik.gui.panels;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import pl.vanta.mjuzik.formats.IFileInfo;
import pl.vanta.mjuzik.gui.listeners.PlayerLoadedListener;
import pl.vanta.mjuzik.gui.listeners.PlayerProgressListener;
import pl.vanta.mjuzik.model.AudioFile;
import pl.vanta.mjuzik.services.ConfigurationService;
import pl.vanta.utils.gui.panels.VPanel;

public class DetailsPanel extends VPanel implements PlayerProgressListener, PlayerLoadedListener
{
	private static final long	serialVersionUID	= 1L;

	private JLabel				lblCurrentTime;
	private JLabel				lblTotalTime;
	private JLabel				lblProgress;
	private JLabel				lblArtist;
	private JLabel				lblAlbum;
	private JLabel				lblTitle;
	private JLabel				lblTrack;

	private JLabel				lblCurrentTimeText;
	private JLabel				lblTotalTimeText;
	private JLabel				lblProgressText;
	private JLabel				lblArtistText;
	private JLabel				lblAlbumText;
	private JLabel				lblTitleText;
	private JLabel				lblTrackText;

	final ConfigurationService	configurationService;

	public DetailsPanel(ConfigurationService configurationService)
	{
		this.configurationService = configurationService;

		initComponents();
		layoutComponents();
	}

	public void initComponents()
	{
		this.lblCurrentTime = new JLabel("Current time: ", SwingConstants.RIGHT);
		this.lblTotalTime = new JLabel("Total time: ", SwingConstants.RIGHT);
		this.lblProgress = new JLabel("Progress: ", SwingConstants.RIGHT);
		this.lblArtist = new JLabel("Artits: ", SwingConstants.RIGHT);
		this.lblAlbum = new JLabel("Album: ", SwingConstants.RIGHT);
		this.lblTitle = new JLabel("Title: ", SwingConstants.RIGHT);
		this.lblTrack = new JLabel("Track number: ", SwingConstants.RIGHT);

		this.lblCurrentTimeText = new JLabel();
		this.lblTotalTimeText = new JLabel();
		this.lblProgressText = new JLabel();
		this.lblArtistText = new JLabel();
		this.lblAlbumText = new JLabel();
		this.lblTitleText = new JLabel();
		this.lblTrackText = new JLabel();
	}

	public void layoutComponents()
	{
		JPanel labelsPanel = new JPanel(new GridLayout(7, 1));
		JPanel valuesPanel = new JPanel(new GridLayout(7, 1));

		labelsPanel.add(this.lblCurrentTime);
		labelsPanel.add(this.lblTotalTime);
		labelsPanel.add(this.lblProgress);
		labelsPanel.add(this.lblArtist);
		labelsPanel.add(this.lblAlbum);
		labelsPanel.add(this.lblTitle);
		labelsPanel.add(this.lblTrack);

		valuesPanel.add(this.lblCurrentTimeText);
		valuesPanel.add(this.lblTotalTimeText);
		valuesPanel.add(this.lblProgressText);
		valuesPanel.add(this.lblArtistText);
		valuesPanel.add(this.lblAlbumText);
		valuesPanel.add(this.lblTitleText);
		valuesPanel.add(this.lblTrackText);

		this.setLayout(new BorderLayout());
		this.add(labelsPanel, BorderLayout.WEST);
		this.add(valuesPanel, BorderLayout.CENTER);
	}

	@Override
	public void updateProgress(long time, float percent, int bytes)
	{
		this.lblProgressText.setText(Math.round(percent) + "%");

		this.lblCurrentTimeText.setText(this.configurationService.getOptions().getTimeFormat().format(time));
	}

	@Override
	public void playerLoaded(AudioFile audioFile)
	{
		FileInfo fileInfo = audioFile.getFileInfo();

		this.lblTotalTimeText.setText(this.configurationService.getOptions().getTimeFormat().format(fileInfo.getPlayTime()));

		this.lblArtistText.setText(fileInfo.getArtist());
		this.lblAlbumText.setText(fileInfo.getAlbum());
		this.lblTitleText.setText(fileInfo.getTitle());
		this.lblTrackText.setText(String.valueOf(fileInfo.getTrack()));

	}

	@Override
	public void playerUnloaded()
	{
		this.lblCurrentTimeText.setText("");
		this.lblTotalTimeText.setText("");
		this.lblProgressText.setText("");

		this.lblArtistText.setText("");
		this.lblAlbumText.setText("");
		this.lblTitleText.setText("");
		this.lblTrackText.setText("");
	}
}